﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using System.Diagnostics;

namespace WinFormStudy
{
    public partial class Form_PDFSplit : Form
    {
        public Form_PDFSplit()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            this.Show();

        }

        private void btnChooseFile_Click(object sender, EventArgs e)
        {
            //初始化一个OpenFileDialog类
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Filter = @"PDF文件|*.pdf;*.PDF";
            //判断用户是否正确的选择了文件
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                //获取用户选择文件的后缀名
                string extension = Path.GetExtension(fileDialog.FileName);
                //声明允许的后缀名
                string[] str = new string[] { ".pdf", ".PDF"};
                if (!((IList)str).Contains(extension))
                {
                    MessageBox.Show("仅能上传pdf文件！");
                }
                else
                { 
                    //获取用户选择的文件
                    FileInfo fileInfo = new FileInfo(fileDialog.FileName);
                    tbFilePath.Text = fileDialog.FileName;
                }
            }
        }

        private void btnTarget_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folder = new FolderBrowserDialog();
            if (folder.ShowDialog() == DialogResult.OK)
            {
                string path = folder.SelectedPath;
                tbTarget.Text = path;
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            lbTime.Text = "0";
            lbStatus.Text = "";

            string source = tbFilePath.Text;
            string target = tbTarget.Text;
            if (source.Length == 0 || target.Length == 0)
            {
                MessageBox.Show("pdf数据源与目标文件夹不得为空");
            }
            else
            {
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                lbStatus.Text = "生成中...";
                PDFKit.PDFSplit(source, target);
                stopwatch.Stop();
                lbStatus.Text = "已生成";
                lbTime.Text = stopwatch.Elapsed.TotalSeconds.ToString()+"秒";
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
